package com.aspose.cells;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class DataSorter {
   Workbook a;
   private Object c;
   private Cells d;
   private CellArea e;
   private boolean f;
   private ArrayList g = new ArrayList();
   String b;
   private boolean h;
   private boolean i;
   private boolean j;

   DataSorter(Object parent) {
      this.a(parent);
   }

   ArrayList a() {
      return this.g;
   }

   CellArea b() {
      if (this.f) {
         return this.e;
      } else {
         return this.c instanceof AutoFilter ? ((AutoFilter)this.c).b() : this.e;
      }
   }

   void a(CellArea var1) {
      this.f = true;
      this.e = var1;
      this.g.clear();
   }

   Object c() {
      return this.c;
   }

   void a(Object var1) {
      this.c = var1;
      if (var1 instanceof Workbook) {
         this.a = (Workbook)var1;
      } else if (var1 instanceof AutoFilter) {
         AutoFilter var2 = (AutoFilter)var1;
         this.d = var2.a().getCells();
         this.a = var2.a().getWorkbook();
      }

   }

   public void clear() {
      this.j = false;
      this.h = false;
      this.g = new ArrayList();
   }

   public boolean hasHeaders() {
      return this.h;
   }

   public void setHasHeaders(boolean value) {
      this.h = value;
   }

   public void addKey(int key, int order) {
      zql var3 = new zql(this);
      var3.b(key);
      var3.a(order);
      com.aspose.cells.b.a.a.ze.a(this.g, (Object)var3);
   }

   public void addKey(int key, int order, String customList) {
      zql var4 = new zql(this);
      var4.b(key);
      var4.a(order);
      var4.a(customList);
      com.aspose.cells.b.a.a.ze.a(this.g, (Object)var4);
   }

   void a(zql var1) {
      com.aspose.cells.b.a.a.ze.a(this.g, (Object)var1);
   }

   public int getKey1() {
      return this.g.size() > 0 ? ((zql)this.g.get(0)).c() : -1;
   }

   public void setKey1(int value) {
      zql var2;
      if (this.g.size() > 0) {
         var2 = (zql)this.g.get(0);
         var2.b(value);
      } else {
         var2 = new zql(this);
         var2.b(value);
         com.aspose.cells.b.a.a.ze.a(this.g, (Object)var2);
      }

   }

   public int getOrder1() {
      return this.g.size() > 0 ? ((zql)this.g.get(0)).b() : 0;
   }

   public void setOrder1(int value) {
      zql var2;
      if (this.g.size() > 0) {
         var2 = (zql)this.g.get(0);
         var2.a(value);
      } else {
         var2 = new zql(this);
         var2.a(value);
         com.aspose.cells.b.a.a.ze.a(this.g, (Object)var2);
      }

   }

   public int getKey2() {
      return this.g.size() > 1 ? ((zql)this.g.get(1)).c() : -1;
   }

   public void setKey2(int value) {
      zql var2;
      if (this.g.size() > 1) {
         var2 = (zql)this.g.get(1);
         var2.b(value);
      } else {
         var2 = new zql(this);
         var2.b(value);
         com.aspose.cells.b.a.a.ze.a(this.g, (Object)var2);
      }

   }

   public int getOrder2() {
      return this.g.size() > 1 ? ((zql)this.g.get(1)).b() : 0;
   }

   public void setOrder2(int value) {
      zql var2;
      if (this.g.size() > 1) {
         var2 = (zql)this.g.get(1);
         var2.a(value);
      } else {
         var2 = new zql(this);
         var2.a(value);
         com.aspose.cells.b.a.a.ze.a(this.g, (Object)var2);
      }

   }

   public int getKey3() {
      return this.g.size() > 2 ? ((zql)this.g.get(2)).c() : -1;
   }

   public void setKey3(int value) {
      zql var2;
      if (this.g.size() > 2) {
         var2 = (zql)this.g.get(2);
         var2.b(value);
      } else {
         var2 = new zql(this);
         var2.b(value);
         com.aspose.cells.b.a.a.ze.a(this.g, (Object)var2);
      }

   }

   public int getOrder3() {
      return this.g.size() > 2 ? ((zql)this.g.get(2)).b() : 0;
   }

   public void setOrder3(int value) {
      zql var2;
      if (this.g.size() > 2) {
         var2 = (zql)this.g.get(2);
         var2.a(value);
      } else {
         var2 = new zql(this);
         var2.a(value);
         com.aspose.cells.b.a.a.ze.a(this.g, (Object)var2);
      }

   }

   public boolean getSortLeftToRight() {
      return this.i;
   }

   public void setSortLeftToRight(boolean value) {
      this.i = value;
   }

   public boolean getCaseSensitive() {
      return this.j;
   }

   public void setCaseSensitive(boolean value) {
      this.j = value;
   }

   public void sort(Cells cells, int startRow, int startColumn, int endRow, int endColumn) {
      CellArea var6 = new CellArea();
      var6.StartRow = startRow;
      var6.StartColumn = startColumn;
      var6.EndRow = endRow;
      var6.EndColumn = endColumn;
      this.sort(cells, var6);
   }

   public void sort(Cells cells, CellArea area) {
      this.d = cells;
      this.e = area;
      this.sort();
      this.d = null;
   }

   public void sort() {
      if (this.c instanceof AutoFilter) {
         AutoFilter var1 = (AutoFilter)this.c;
         this.e = var1.b();
      }

      this.d.r().n().d();

      for(int var17 = 0; var17 < this.g.size(); ++var17) {
         zql var2 = (zql)this.g.get(var17);
         if (var2.c() < 0) {
            this.g.remove(var17--);
         }
      }

      if (this.g.size() != 0) {
         if (this.i) {
            this.e();
         } else {
            this.d.w();
            ArrayList var19 = new ArrayList();
            int var18 = this.h ? this.e.StartRow + 1 : this.e.StartRow;
            int var3 = -1;
            int var4 = -1;
            int var5 = -1;

            int var6;
            for(var6 = var18; var6 <= this.e.EndRow; ++var6) {
               Row var7 = this.d.getRows().a(var6, true, false, false);
               if (var7 != null && var7.h() != 0 && (var7.getIndex() == var18 || var7.getIndex() == var18 + 1)) {
                  var4 = var5 = var7.getIndex();
                  ++var6;

                  while(var6 <= this.e.EndRow) {
                     var7 = this.d.getRows().a(var6, true, false, false);
                     if (var7 == null || var7.h() == 0) {
                        var3 = var7.getIndex();
                        break;
                     }

                     var5 = var6++;
                  }

                  if (var5 == this.e.EndRow) {
                     var5 = -1;
                     var4 = -1;

                     for(var6 = var18; var6 < this.e.EndRow; ++var6) {
                        com.aspose.cells.b.a.a.ze.a(var19, (Object)var6);
                     }
                  }
               }

               com.aspose.cells.b.a.a.ze.a(var19, (Object)var6);
            }

            this.a(0, (zql)this.g.get(0), var19);
            var6 = var18;
            this.d.n();
            this.d.getRows().d.a();
            HyperlinkCollection var20 = this.d.h().getHyperlinks();
            CellArea[] var8 = new CellArea[var20.getCount()];

            for(int var9 = 0; var9 < var8.length; ++var9) {
               var8[var9] = var20.get(var9).getArea();
            }

            RowCollection var21 = new RowCollection(this.d, this.d.a(this.e.EndRow - this.e.StartRow + 1, 10, this.e.EndColumn - this.e.StartColumn + 1, 5));
            Row var10 = null;

            int var11;
            int var13;
            for(var11 = var18; var11 <= this.e.EndRow; ++var11) {
               Row var12 = this.d.getRows().a(var11, true, false, false);
               if (var12 != null) {
                  var10 = var21.a(var11, Math.min(var12.a(), this.e.EndColumn - this.e.StartColumn + 1));
                  var10.a(var12);
                  if (var12.getIndex() >= var4 && var12.getIndex() <= var5) {
                     var12.a((byte)0);
                  }

                  var13 = -1;
                  int var14 = -1;

                  for(int var15 = 0; var15 < var12.a(); ++var15) {
                     Cell var16 = var12.getCellByIndex(var15);
                     if (var16.getColumn() >= this.e.StartColumn) {
                        if (var16.getColumn() > this.e.EndColumn) {
                           break;
                        }

                        if (var13 == -1) {
                           var13 = var15;
                        }

                        var14 = var15;
                        var10.a.a(-1, var16.c);
                     }
                  }

                  if (var13 != -1) {
                     var12.a(var13, var14 - var13 + 1);
                  }
               }
            }

            for(var11 = 0; var11 < var19.size(); ++var11) {
               int var22 = (Integer)var19.get(var11);
               if (var22 == var3) {
                  for(var13 = var4; var13 <= var5; ++var13) {
                     var10 = var21.a(var13, true, false, false);
                     this.a(var10, var6, this.e.StartColumn, this.e.EndColumn, true, var8);
                     ++var6;
                  }
               }

               var10 = var21.a(var22, true, false, false);
               this.a(var10, var6, this.e.StartColumn, this.e.EndColumn, false, var8);
               ++var6;
            }

            for(var11 = 0; var11 < var8.length; ++var11) {
               var20.get(var11).a(var8[var11]);
            }

         }
      }
   }

   private void a(Row var1, int var2, int var3, int var4, boolean var5, CellArea[] var6) {
      if (var1 != null) {
         HyperlinkCollection var7 = this.d.h().getHyperlinks();

         for(int var8 = 0; var8 < var7.getCount(); ++var8) {
            Hyperlink var9 = var7.get(var8);
            CellArea var10 = var9.getArea();
            if (var10.EndRow == var10.StartRow && var10.StartRow == var1.getIndex() && var10.StartColumn >= var3 && var10.EndColumn <= var4) {
               var10.StartRow = var10.EndRow = var2;
               var6[var8] = var10;
            }
         }

         Row var12 = null;
         if (var5) {
            var12 = this.d.getRows().a(var2, false, true, false);
            var12.a(var1.h());
         }

         if (var1.j() || var1.a() != 0) {
            if (var12 == null) {
               var12 = this.d.getRows().a(var2, false, true, false);
            }

            Cell var11;
            int var13;
            Cell var14;
            if (var1.j()) {
               for(var13 = var3; var13 <= var4; ++var13) {
                  var14 = var12.c(var13);
                  var11 = var1.getCellOrNull(var13);
                  if (var11 == null) {
                     var14.b(var1.d());
                  } else {
                     this.a(var11, var14);
                  }
               }
            } else {
               for(var13 = var3; var13 <= var4; ++var13) {
                  var14 = var1.getCellOrNull(var13);
                  if (var14 != null) {
                     var11 = var12.c(var13);
                     this.a(var14, var11);
                  }
               }
            }

         }
      }
   }

   private int a(ArrayList var1, int var2) {
      for(int var3 = 0; var3 < var1.size(); ++var3) {
         if ((Integer)var1.get(var3) == var2) {
            return var3;
         }
      }

      return -1;
   }

   private void a(Cell var1, Cell var2) {
      var2.b(var1.s());
      if (var1.f()) {
         zyi var3 = var1.g();
         var3.e = var2.getRow();
         var3.f = var2.c.a;
         var2.a(5, var3, false);
         byte[] var4;
         if (var1.isArrayHeader()) {
            if (var1.getRow() != var2.getRow()) {
               var4 = var2.v();
               zyo.b(this.d.h(), true, 0, var2.getRow() - var1.getRow(), 0, 0, -1, -1, var4);
               zyo.b(this.d.h(), true, 0, var2.getRow() - var1.getRow(), 0, 0, -1, -1, var2.D().d());
               boolean var5 = false;
               boolean[] var6 = new boolean[]{var5};
               CellArea var7 = zayd.a(var2.D().a(), 0, var2.getRow() - var1.getRow(), var6);
               var5 = var6[0];
               var2.D().a(var7);
            }
         } else if (var1.getRow() != var2.getRow()) {
            var4 = var2.v();
            zyo.b(this.d.h(), true, 0, var2.getRow() - var1.getRow(), 0, 0, -1, -1, var4);
         }

      } else {
         var2.a((zfv)var1.c, 6, false);
      }
   }

   private int d() {
      return this.g.size() - 1;
   }

   private void a(com.aspose.cells.b.a.a.zd var1, int var2, zql var3, ArrayList var4) {
      Iterator var5;
      ArrayList var6;
      if (var3 != null) {
         var5 = var1.f().iterator();

         while(var5.hasNext()) {
            var6 = (ArrayList)var5.next();
            if (var6.size() == 1) {
               com.aspose.cells.b.a.a.ze.a(var4, var6.get(0));
            } else {
               this.a(var2, var3, var6);
               com.aspose.cells.b.a.a.ze.a(var4, (Collection)var6);
            }
         }

      } else {
         var5 = var1.f().iterator();

         while(var5.hasNext()) {
            var6 = (ArrayList)var5.next();
            com.aspose.cells.b.a.a.ze.a(var4, (Collection)var6);
         }

      }
   }

   private void e() {
      this.d.n();
      this.d.getRows().d.a();
      ArrayList var1 = new ArrayList();
      int var2 = this.h ? this.e.StartColumn + 1 : this.e.StartColumn;

      int var3;
      for(var3 = var2; var3 <= this.e.EndColumn; ++var3) {
         com.aspose.cells.b.a.a.ze.a(var1, (Object)var3);
      }

      this.a(0, (zql)this.g.get(0), var1);
      boolean var4 = false;
      boolean var5 = false;

      for(int var6 = 0; var6 < var1.size(); ++var6) {
         int var7 = (Integer)var1.get(var6);
         if (var7 != -1 && var7 != var2 + var6) {
            var3 = var2 + var6;
            ArrayList var8 = new ArrayList();
            int var10 = var3;
            int var9 = var6;
            this.a(this.e.StartRow, this.e.EndRow, var7, var8);

            while(var9 != -1) {
               var8 = this.a(var10, var8, this.e.StartRow, this.e.EndRow);
               var1.set(var9, -1);
               var9 = this.a(var1, var10);
               var10 = var2 + var9;
            }
         } else {
            var1.set(var6, -1);
         }
      }

   }

   private void a(int var1, int var2, int var3, ArrayList var4) {
      zfv var5 = new zfv();
      zahl var7 = this.d.f().b(var1, var2, false);

      while(true) {
         int var8 = var7.a();
         if (var8 < 0) {
            return;
         }

         zp var9 = this.d.f().f(var8);
         var8 = var9.b(var3);
         if (var8 > -1) {
            zfv var6 = var9.a(var8, var5, 7);
            if (var6 == var5) {
               var6 = new zfv(var5);
            }

            com.aspose.cells.b.a.a.ze.a(var4, (Object)(new Cell(new Row(this.d, this.d.getRows(), var9, false), var8, var6, false)));
            var9.c(var8, 1);
         }
      }
   }

   private ArrayList a(int var1, ArrayList var2, int var3, int var4) {
      ArrayList var5 = new ArrayList();
      this.a(var3, var4, var1, var5);
      Iterator var6 = var2.iterator();

      while(var6.hasNext()) {
         Cell var7 = (Cell)var6.next();
         this.b(var7, this.d.a(var7.getRow(), var1, false));
      }

      return var5;
   }

   private void b(Cell var1, Cell var2) {
      if (var1.c.c == 5) {
         zyi var3 = (zyi)var1.c.d;
         var3.e = var2.getRow();
         var3.f = var2.c.a;
      }

      var2.a((zfv)var1.c, 6, false);
   }

   void a(int var1, zql var2, ArrayList var3) {
      com.aspose.cells.b.a.a.zd var4 = new com.aspose.cells.b.a.a.zd(new zbnr(var2, this.d));
      ArrayList var5 = new ArrayList();
      this.a(var2, var3, var4, var5);
      boolean var6 = false;
      if (var1 < this.d()) {
         ++var1;
         var2 = (zql)this.g.get(var1);
         var6 = true;
      } else {
         var2 = null;
      }

      var3.clear();
      this.a(var4, var1, var2, var3);
      if (var6) {
         this.a(var1, var2, var5);
      }

      com.aspose.cells.b.a.a.ze.a(var3, (Collection)var5);
   }

   void a(zql var1, ArrayList var2, com.aspose.cells.b.a.a.zd var3, ArrayList var4) {
      zbns var5 = null;
      int var6 = var1.c();
      Iterator var7 = var2.iterator();

      while(var7.hasNext()) {
         int var8 = (Integer)var7.next();
         if (this.i) {
            var5 = this.a(var1, var6, var8);
         } else {
            var5 = this.a(var1, var8, var6);
         }

         if (var5 == null) {
            com.aspose.cells.b.a.a.ze.a(var4, (Object)var8);
         } else {
            Object var9 = var3.d((Object)var5);
            ArrayList var10;
            if (var9 == null) {
               var10 = new ArrayList();
               com.aspose.cells.b.a.a.ze.a(var10, (Object)var8);
               var3.a(var5, var10);
            } else {
               var10 = (ArrayList)var9;
               com.aspose.cells.b.a.a.ze.a(var10, (Object)var8);
            }
         }
      }

   }

   private zbns a(zql var1, int var2, int var3) {
      Cell var4 = this.d.a(var2, var3, true);
      if (var4 == null) {
         return null;
      } else {
         zbns var5;
         switch(var4.getType()) {
         case 0:
            var5 = new zbns();
            var5.b = var4.getValue();
            var5.a = 0;
            var5.c = 2;
            return var5;
         case 1:
         case 4:
            double var8 = var4.getDoubleValue();
            zbns var7 = new zbns();
            var7.b = var8;
            var7.a = 4;
            var7.c = 0;
            return var7;
         case 2:
            var5 = new zbns();
            var5.b = var4.getStringValue();
            var5.a = 2;
            var5.c = 3;
            return var5;
         case 3:
            return null;
         default:
            String var9 = var4.getStringValue();
            zbns var6 = new zbns();
            var6.b = this.getCaseSensitive() ? var4.getStringValue() : var4.getStringValue().toUpperCase();
            var6.a = 5;
            var6.c = 2;
            if (var1.f() != null && var1.f().get(var9) != null) {
               var6.d = (Integer)var1.f().get(var9);
            }

            return var6;
         }
      }
   }
}
